package roborally.exceptions;

import be.kuleuven.cs.som.annotate.*;
import roborally.model.Element;

/**
 * A class for signaling illegal sharing of positions between elements on a board.
 * 
 * @author 	Pieter Robberechts
 * @author 	Jeroen Sanders
 */
public class PositionSharingException extends RuntimeException {

	/**
	 * Initialize this new PositionSharingException with a message.
	 * 
	 * @param 	message
	 * 			The message associated with this exception.
	 * @post	...
	 * 			| new.getMessage == message
	 */
	public PositionSharingException(String message) {
		this.firstElement = null;
		this.secondElement = null;
		this.message = message;
	}
	
	/**
	 * Initialize this new PositionSharingException with both elements involved.
	 * 
	 * @param  	first
	 *         	The first element involved in this exception.
	 * @param	second
	 * 			The second element involved in this exception.        
	 * @post   	...
	 * 			| new.getFirstElement() == first
	 * @post	...
	 * 			| new.getSecondElement() == second
	 */
	public PositionSharingException(Element first, Element second) {
		this.firstElement = first;
		this.secondElement = second;
		this.message = null;
	}

	/**
	 * Return a message associated with this exception.
	 */
	@Immutable
	public String getMessage() {
		if(message != null)
			return message;
		else			
			return getFirstElement().toString() + " and " + getSecondElement().toString() + " can't share the same position.";
	}
	
	/**
	 * Variable registering a message associated with this exception.
	 */
	private final String message;
	
	/**
	 * Returns the first element involved in this exception.
	 */
	@Basic @Immutable
	public Element getFirstElement() {
		return firstElement;
	}
	
	/**
	 * Variable registering the first element involved in this exception.
	 */
	private final Element firstElement;
	
	/**
	 * Returns the second element involved in this exception.
	 */
	@Basic @Immutable
	public Element getSecondElement() {
		return secondElement;
	}
	
	/**
	 * Variable registering the second element involved in this exception.
	 */
	private final Element secondElement;
	
	private static final long serialVersionUID = 699697645719133436L;

}
